//
//  LAReviewHeaderView.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/11/2.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAReviewHeaderView.h"
#import "LAReviewHeaderViewButton.h"
#import "LZReviewCountModel.h"
#import "UIScrollView+LZContentOffset.h"
#import "Masonry.h"

@interface LAReviewHeaderView ()<UIScrollViewDelegate>

@property (nonatomic, strong) LAReviewHeaderViewButton *btn_all;
@property (nonatomic, strong) LAReviewHeaderViewButton *btn_good;
@property (nonatomic, strong) LAReviewHeaderViewButton *btn_bad;
@property (nonatomic, strong) LAReviewHeaderViewButton *btn_picture;
@property (nonatomic, strong) UIControl *selectedButton;
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, assign) CGSize size_all;
@property (nonatomic, assign) CGSize size_good;
@property (nonatomic, assign) CGSize size_bad;
@property (nonatomic, assign) CGSize size_picture;

@end

@implementation LAReviewHeaderView

-(instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createView];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self.scroll setLanguageContentOffset];
}

- (void)setModel:(LZReviewCountModel *)model {
    _model = model;
    if (model) {
        self.btn_all.num.text = [NSString stringWithFormat:@"%ld",model.total];
        self.btn_good.num.text = [NSString stringWithFormat:@"%ld",model.good];
        self.btn_bad.num.text = [NSString stringWithFormat:@"%ld",model.bad];
        self.btn_picture.num.text = [NSString stringWithFormat:@"%ld",model.hasPhoto];
    }
}

-(void)createView {
    self.scroll = [[UIScrollView alloc] init];
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.delegate = self;
    self.scroll.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.scroll];
    [self.scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    UIView *container = [[UIView alloc] init];
    [self.scroll addSubview:container];
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scroll);
        make.height.equalTo(self);
    }];
    
    self.btn_all = [[LAReviewHeaderViewButton alloc] init];
    self.btn_all.tag = 0;
    self.btn_all.title.text = LZLocalizedString(@"review_all", nil);    // 全部
    self.btn_all.num.text = @"0";
    [self.btn_all addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [container addSubview:self.btn_all];
    self.btn_all.selected = YES;
    self.selectedButton = _btn_all;
    self.size_all = [self.btn_all intrinsicContentSize];
    
    self.btn_good = [[LAReviewHeaderViewButton alloc] init];
    self.btn_good.tag = 1;
    self.btn_good.title.text = LZLocalizedString(@"review_good", nil);  // 好评
    self.btn_good.num.text = @"0";
    [self.btn_good addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [container addSubview:self.btn_good];
    self.size_good = [self.btn_good intrinsicContentSize];
    
    self.btn_bad = [[LAReviewHeaderViewButton alloc] init];
    self.btn_bad.tag = 2;
    self.btn_bad.title.text = LZLocalizedString(@"review_bad", nil);    // 差评
    self.btn_bad.num.text = @"0";
    [self.btn_bad addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [container addSubview:self.btn_bad];
    self.size_bad = [self.btn_bad intrinsicContentSize];
    
    self.btn_picture = [[LAReviewHeaderViewButton alloc] init];
    self.btn_picture.tag = 3;
    self.btn_picture.title.text = LZLocalizedString(@"review_with_picture", nil);   // 有图
    self.btn_picture.num.text = @"0";
    [self.btn_picture addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [container addSubview:self.btn_picture];
    self.size_picture = [self.btn_picture intrinsicContentSize];
    
    CGFloat padding = 15; // (SCREEN_WIDTH - 20 - size_all.width - size_good.width - size_bad.width - size_picture.width)/3.0;
    
    CGFloat moreSpace = SCREEN_WIDTH - self.size_all.width - self.size_good.width - self.size_bad.width - self.size_picture.width - 10 - padding*3 - 10;
    if (moreSpace>0) {
        moreSpace = moreSpace/4.0;
    }
    else {
        moreSpace = 0;
    }
    
    
    [self.btn_all mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(container).offset(20);
        make.leading.equalTo(container).offset(10);
        make.height.mas_equalTo(self.size_all.height);
        make.width.mas_equalTo(self.size_all.width+moreSpace);
    }];
    
    [self.btn_good mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btn_all);
        make.height.mas_equalTo(self.size_good.height);
        make.width.mas_equalTo(self.size_good.width+moreSpace);
        make.leading.equalTo(self.btn_all.mas_trailing).offset(padding);
    }];
    
    [self.btn_bad mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btn_all);
        make.height.mas_equalTo(self.size_bad.height);
        make.width.mas_equalTo(self.size_bad.width+moreSpace);
        make.leading.equalTo(self.btn_good.mas_trailing).offset(padding);
    }];
    
    [self.btn_picture mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btn_all);
        make.height.mas_equalTo(self.size_picture.height);
        make.width.mas_equalTo(self.size_picture.width+moreSpace);
        make.leading.equalTo(self.btn_bad.mas_trailing).offset(padding);
    }];
    
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.btn_picture.mas_trailing).offset(10);
    }];
    
}

-(CGSize)intrinsicContentSize {
    return CGSizeMake(SCREEN_WIDTH, [self.btn_all intrinsicContentSize].height+40);
}

-(void)buttonClickAction:(UIControl*)sender {

    [self.scroll scrollRectToVisible:CGRectInset(sender.frame, -20, 0)
                            animated:YES];

    if (self.selectedButton == sender) {
        return;
    }
    sender.selected = YES;
    self.selectedButton.selected = NO;
    self.selectedButton = sender;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickButton:)]) {
        [self.delegate clickButton:sender];
    }
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
